package wms2010.gui;

import java.awt.BorderLayout;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import wms2010.gui.forms.ageClass.AgeClassFormCreate;
import wms2010.gui.forms.disciplines.DisciplineFormCreate;
import wms2010.gui.forms.swimmers.SwimmerFormCreate;
import wms2010.gui.views.ageClass.AgeClassView;
import wms2010.gui.views.disciplines.DisciplineView;

@Component
public class MainFrame extends JFrame {
	private JTree navigationJTree;
	private JDesktopPane desktopPane = new JDesktopPane();

	@Autowired
	public MainFrame(final AgeClassFormCreate ageClassForm,
			final SwimmerFormCreate swimmerForm, final AgeClassView ageClassView,
			final DisciplineFormCreate disciplineForm, final DisciplineView disciplineView) {
		desktopPane.add(ageClassForm);
		desktopPane.add(swimmerForm);
		desktopPane.add(ageClassView);
		desktopPane.add(disciplineForm);
		desktopPane.add(disciplineView);

		RunnableTreeNode ageClass = new RunnableTreeNode(new Runnable() {
			public void run() {

			}
		}, "Age Classes");
		ageClass.add(new RunnableTreeNode(new Runnable() {
			public void run() {
				ageClassForm.show();
			}
		}, "Create Age Class"));
		ageClass.add(new RunnableTreeNode(new Runnable() {

			public void run() {
				ageClassView.show();
			}
		}, "View Age classes"));

		RunnableTreeNode disciplines = new RunnableTreeNode(new Runnable() {

			public void run() {
				// TODO Auto-generated method stub

			}
		}, "Disciplines");
		disciplines.add(new RunnableTreeNode(new Runnable() {

			public void run() {
				disciplineForm.show();
			}
		}, "Create Discipline"));
		disciplines.add(new RunnableTreeNode(new Runnable() {

			public void run() {
				disciplineView.show();
			}
		}, "View Disciplines"));

		RunnableTreeNode swimmers = new RunnableTreeNode(new Runnable() {

			public void run() {
				// TODO Auto-generated method stub

			}
		}, "Swimmers");
		swimmers.add(new RunnableTreeNode(new Runnable() {

			public void run() {
				swimmerForm.show();
			}
		}, "Create Swimmers"));

		DefaultMutableTreeNode root = new DefaultMutableTreeNode(
				"DLRG WMS 2010");
		// root.add(devices);
		// root.add(track);
		root.add(ageClass);
		root.add(swimmers);
		root.add(disciplines);
		// root.add(competition);

		this.navigationJTree = new JTree(root);

		setTitle("DLRG WMS 2010");
		setSize(300, 200);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		this.add(this.navigationJTree, BorderLayout.WEST);
		this.add(desktopPane, BorderLayout.CENTER);

		this.navigationJTree.getSelectionModel().addTreeSelectionListener(
				new TreeSelectionListener() {
					public void valueChanged(TreeSelectionEvent arg0) {
						RunnableTreeNode node = (RunnableTreeNode) navigationJTree
								.getLastSelectedPathComponent();
						node.run();
					}
				});

	}
}
